//
//  MenuCell.swift
//  ams
//
//  Created by yangyuan on 2017/6/21.
//  Copyright © 2017年 coterjiesen. All rights reserved.
//

import UIKit

class BottonImageView: UIView{
    var lable: UILabel?
    var image: UIImageView?
    var button: UIButton?
    override init(frame : CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    func setupUI(){
        button = UIButton()
        image = UIImageView()
        lable = UILabel()
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor(white: 1, alpha: 0.5).cgColor;
        self.addSubview(button!)
        self.addSubview(image!)
        self.addSubview(lable!)

        self.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(self)
        }
        image!.snp.makeConstraints { (make) in
            make.left.top.equalTo(8)
        }
        lable!.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(-8)
        }
        button!.snp.makeConstraints{ (	make) in
            make.left.top.right.bottom.equalTo(self)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class MenuViewCell: UITableViewCell{
    let heihtMenu = 3 * (SCREEN_WIDTH / 2 - 12) / 4
    let widthMenu = SCREEN_WIDTH / 2 - 12
    
    var myInfoView :BottonImageView?
    var outBinView :BottonImageView?
    var checkBinView :BottonImageView?
    var reportView :BottonImageView?
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    func setupUI(){
        self.backgroundColor = UIColor.clear
        self.selectedBackgroundView?.backgroundColor = UIColor.clear
        self.selectionStyle = .none
        myInfoView = BottonImageView()
        myInfoView?.image?.image = UIImage(named: "ic_person_info")
        myInfoView?.button?.addTarget(self, action: #selector(clickPersonInfo), for: .touchUpInside)
        myInfoView!.button!.backgroundColor = colorWith255RGB(204, g: 154, b:63)
        myInfoView?.lable?.text = "个人中心"
        myInfoView!.lable?.textColor = .white
        outBinView = BottonImageView(frame: CGRect(x: 0, y: 0, width: widthMenu, height: heihtMenu))
        outBinView?.image?.image = UIImage(named: "ic_out_bin_b")
        outBinView?.button?.addTarget(self, action: #selector(click), for: .touchUpInside)
        outBinView!.button!.backgroundColor = colorWith255RGB(99, g: 139, b:146)
        outBinView?.lable?.text = "设备领用"
        outBinView!.lable?.textColor = .white
        checkBinView = BottonImageView(frame: CGRect(x: 0, y: 0, width: widthMenu, height: heihtMenu))
        checkBinView?.image?.image = UIImage(named: "ic_check_bin_b")
        checkBinView?.button?.addTarget(self, action: #selector(click), for: .touchUpInside)
        checkBinView!.button!.backgroundColor = colorWith255RGB(246, g: 96, b:41)
        checkBinView?.lable?.text = "设备盘点"
        checkBinView!.lable?.textColor = .white
        reportView = BottonImageView(frame: CGRect(x: 0, y: 0, width: widthMenu, height: heihtMenu))
        reportView?.image?.image = UIImage(named: "ic_error_bin_b")
        reportView?.button?.addTarget(self, action: #selector(click), for: .touchUpInside)
        reportView!.button!.backgroundColor = colorWith255RGB(181, g: 144, b:246)
        reportView?.lable?.text = "设备报障"
        reportView!.lable?.textColor = .white
        
        self.contentView.addSubview(myInfoView!)
        self.contentView.addSubview(outBinView!)
        self.contentView.addSubview(checkBinView!)
        self.contentView.addSubview(reportView!)
        
        setupLayout()
    }
    func click(){
        print("button clicked !")
    }
    func clickPersonInfo(){
        let vc = PersonInfoViewController()
        vc.hidesBottomBarWhenPushed = true
        CuClient.sharedInstance.centerViewController?.navigationController?.pushViewController(vc, animated: true)
    }
    func setupLayout(){

        self.contentView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalTo(self)
        }
        myInfoView!.snp.makeConstraints { (make) in
            make.left.top.equalTo(self.contentView).offset(6)
            make.height.equalTo(80)
            make.width.equalTo(SCREEN_WIDTH / 2 - 10	)
        }
        outBinView!.snp.makeConstraints { (make) in
            make.top.equalTo(self.myInfoView!)
            make.left.equalTo(self.myInfoView!.snp.right).offset(4)
            make.height.equalTo(80)
            make.width.equalTo(SCREEN_WIDTH / 2 - 10	)
        }
        checkBinView!.snp.makeConstraints { (make) in
            make.left.equalTo(self.myInfoView!)
            make.top.equalTo(self.myInfoView!.snp.bottom).offset(4)
            make.height.equalTo(80)
            make.width.equalTo(SCREEN_WIDTH / 2 - 10	)
        }
        reportView!.snp.makeConstraints { (make) in
            make.left.equalTo(self.outBinView!)
            make.top.equalTo(self.checkBinView!)
            make.height.equalTo(80)
            make.width.equalTo(SCREEN_WIDTH / 2 - 10	)
        }
    }
}
